package com.javaxiaobang.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 功    能：
 * 作    者：tianqiang
 * 时    间：2019/11/17
 */
@Slf4j
@Service
public class RibbonService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hystrixErr",
            // 配置线程池
            threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "1"),
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "1") },
            // 配置命令执行相关的，示例：超时时间
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3500") })
    public Object getDictList(String param) {
        log.info("方法执行开始-{}", param);
        Object result = restTemplate.getForObject("http://clientapp/getDictList", Object.class);
        log.info("方法执行完成-{}-结果:{}", param, result);
        return result;
    }

    public String hystrixErr(String param, Throwable e) {
        String result = "方法执行熔断-" + param;
        log.info(result);
        return result;
    }


}
